#include "SparseMatrix.h"


SparseMatrix::SparseMatrix(int rows,long long cols)
{
	ROWS=rows;
	COLS=cols;
}


SparseMatrix::~SparseMatrix(void)
{

}


MatrixObject SparseMatrix::s_n(const MatrixObject& A,const MatrixObject& B){
	MatrixObject result(ROWS,A.COLS);
	int	flag=0;
	while(flag<Size()){
		for(int i=0;i<result.COLS;i++){
			double ans=result.getElement(getX(flag),i);
			ans+=getVal(flag) * A.m_matrix[ getY(flag)/A.ROWS ][ i ] * B.m_matrix[getY(flag)%B.ROWS][i];
			result.setElement(getX(flag),i,ans);
		}
		flag++;
	}
	return result;
}

void  SparseMatrix::WeightedElementwiseProduct(const MatrixObject &A,const MatrixObject &B,const MatrixObject &C,SparseMatrix &result)
{
	//SparseMatrix result(ROWS,COLS);
	result.COLS=COLS;
	result.ROWS=ROWS;
	result.SM_pos[0]=SM_pos[0];
	result.SM_pos[1]=SM_pos[1];
	result.SM_val=SM_val;

	int k=A.COLS;
	for(int i=0;i<SM_val.size();i++){
		int x=SM_pos[0][i];long long y=SM_pos[1][i];
		double ans=0;	
		for(int j=0;j<k;j++){
			ans+=A.m_matrix[x][j]* B.m_matrix[y/B.ROWS][j]* C.m_matrix[y%C.ROWS][j];
		}
		//	cout<<ans<<" "<<endl;
		result.SM_val[i]=ans;
	}

	//return result;

}


void SparseMatrix::Output() const
{
	for(int i=0;i<SM_val.size();i++){
		cout<<SM_pos[0][i]<<" "<<SM_pos[1][i]<<" is "<<SM_val[i]<<endl;
	}
}


